/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CargaAmigo.java
 *
 * Created on Mar 3, 2011, 10:50:06 AM
 */

package forms;

import beans.Persona;
import dao.PersonaDao;
import exceptions.DAOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Guido Cresta
 */
public class CargaAmigo extends javax.swing.JFrame {

    /** Creates new form CargaAmigo */
    public CargaAmigo() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btn_agregar = new javax.swing.JButton();
        txt_nombre = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txt_apellido = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txt_telefono = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        date_fecha_nacimiento = new com.toedter.calendar.JDateChooser();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        btn_agregar.setText("Agregar");
        btn_agregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_agregarActionPerformed(evt);
            }
        });

        jLabel1.setText("Nombre:");

        jLabel2.setText("Apellido:");

        jLabel3.setText("Teléfono:");

        jLabel4.setText("Fecha Nac:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(514, Short.MAX_VALUE)
                .addComponent(btn_agregar)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addGap(46, 46, 46)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(date_fecha_nacimiento, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txt_telefono)
                    .addComponent(txt_apellido)
                    .addComponent(txt_nombre))
                .addContainerGap(394, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(60, 60, 60)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txt_nombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txt_apellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txt_telefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(date_fecha_nacimiento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 282, Short.MAX_VALUE)
                .addComponent(btn_agregar)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btn_agregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_agregarActionPerformed
        if(validarSiguiente()==true){
            Persona persona = new Persona();
            persona.setApellido(txt_apellido.getText());
            persona.setFechaNacimiento(date_fecha_nacimiento.getDate());
            persona.setNombre(txt_nombre.getText());
            persona.setTelefono(txt_telefono.getText());

            PersonaDao personaDao = new PersonaDao();
            boolean exito = personaDao.saveOrUpdate(persona);
            if(exito==true){
                JOptionPane.showMessageDialog(null, "Amigo guardado!", "Aviso", JOptionPane.OK_OPTION);
            }else{
                JOptionPane.showMessageDialog(null, "No se pudo guardar, intente nuevamente", "Error", JOptionPane.OK_OPTION);
            }
            
            
            int eleccion = JOptionPane.showOptionDialog(null,"Desea ingresar otro amigo?", "Selector de opciones",
                    JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE,null, new Object[] {"Si, "
                            + "agregar otro amigo", "no, guardar el amigo" }, "Si, agregar otro amigo");

            if(eleccion == 1){
                this.setVisible(false);
            }else{
                this.setVisible(false);
                CargaAmigo ca = new CargaAmigo();
                ca.setVisible(true);
            }

        }
    }//GEN-LAST:event_btn_agregarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CargaAmigo().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_agregar;
    private com.toedter.calendar.JDateChooser date_fecha_nacimiento;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField txt_apellido;
    private javax.swing.JTextField txt_nombre;
    private javax.swing.JTextField txt_telefono;
    // End of variables declaration//GEN-END:variables

    public boolean validarSiguiente(){
        boolean valida = true;
       if(txt_nombre.getText().toString().equalsIgnoreCase("")){
           JOptionPane.showMessageDialog(null, "Debe ingresar el nombre!", "Error", JOptionPane.OK_OPTION);
           txt_nombre.requestFocus();
           valida = false;
       }else if(txt_apellido.getText().toString().equalsIgnoreCase("")){
           JOptionPane.showMessageDialog(null, "Debe ingresar el apellido!", "Error", JOptionPane.OK_OPTION);
           txt_apellido.requestFocus();
           valida = false;
       }else if(date_fecha_nacimiento.getDate()==null){
           JOptionPane.showMessageDialog(null, "Debe ingresar la fecha de nacimiento!", "Error", JOptionPane.OK_OPTION);
           valida = false;
       }else if(txt_telefono.getText().toString().equalsIgnoreCase("")){
           JOptionPane.showMessageDialog(null, "Debe ingresar el teléfono!", "Error", JOptionPane.OK_OPTION);
           txt_telefono.requestFocus();
           valida = false;
       }else if(!txt_telefono.toString().equalsIgnoreCase("")){
       try{
            Double.parseDouble(txt_telefono.getText().toString());
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "El teléfono debe ser numérico", "Error", JOptionPane.OK_OPTION);
            txt_telefono.setText("");
           valida = false;
           return valida;
           }
        }
       
       return valida;
    }
}
